package appfight.monster;

import org.anddev.andengine.entity.Entity;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.DoubleValueSpanEntityModifier;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.util.modifier.IModifier.IModifierListener;
import org.anddev.andengine.util.modifier.ease.IEaseFunction;

public class HpBar extends Entity{
	
	Rectangle RecBackGround;
	Rectangle RecBackWhite;
	Rectangle RecBackHP;
	
	public int maxHp = 100;
	public int currentHp = 100;
	
	public HpBar()
	{
		RecBackGround = new Rectangle(0,0,154,14);
		RecBackGround.setColor(0, 0, 0);
		
		RecBackWhite = new Rectangle(2,2,150,10);
		RecBackWhite.setColor(1, 1, 1);
		
		RecBackHP = new Rectangle(2,2,150,10);
		RecBackHP.setColor(0, 1, 0);
		
		this.attachChild(RecBackGround);
		this.attachChild(RecBackWhite);
		this.attachChild(RecBackHP);		
	}
	
	public void setMaxHp(int i)
	{
		maxHp = i;
		
		float ratio = (float)currentHp / (float)maxHp;
		RecBackHP.setWidth(ratio*150);
		
	}
	
	public void setCurrentHp(int i)
	{
		currentHp = i;
		
		float ratio = (float)currentHp / (float)maxHp;
		RecBackHP.setHeight(ratio*150);
	};
	
	public void AnimateSetHp(int hp,IModifierListener listen)
	{
		currentHp = hp;
		float ratio = (float)currentHp / (float)maxHp;
		
		SizeModifier s = new SizeModifier(1f, RecBackHP.getWidth(), ratio*150, RecBackHP.getHeight(), RecBackHP.getHeight());
		
		
		if(listen!=null)
		  s.addModifierListener(listen);
		RecBackHP.registerEntityModifier(s);
		
	}
	
	
	
	public class SizeModifier extends DoubleValueSpanEntityModifier {
		// ===========================================================
		// Constants
		// ===========================================================

		// ===========================================================
		// Fields
		// ===========================================================

		// ===========================================================
		// Constructors
		// ===========================================================

		public SizeModifier(final float pDuration, final float pFromScale, final float pToScale) {
			this(pDuration, pFromScale, pToScale, null, IEaseFunction.DEFAULT);
		}

		public SizeModifier(final float pDuration, final float pFromScale, final float pToScale, final IEaseFunction pEaseFunction) {
			this(pDuration, pFromScale, pToScale, null, pEaseFunction);
		}

		public SizeModifier(final float pDuration, final float pFromScale, final float pToScale, final IEntityModifierListener pEntityModifierListener) {
			this(pDuration, pFromScale, pToScale, pFromScale, pToScale, pEntityModifierListener, IEaseFunction.DEFAULT);
		}

		public SizeModifier(final float pDuration, final float pFromScale, final float pToScale, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction) {
			this(pDuration, pFromScale, pToScale, pFromScale, pToScale, pEntityModifierListener, pEaseFunction);
		}

		public SizeModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY) {
			this(pDuration, pFromScaleX, pToScaleX, pFromScaleY, pToScaleY, null, IEaseFunction.DEFAULT);
		}

		public SizeModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final IEaseFunction pEaseFunction) {
			this(pDuration, pFromScaleX, pToScaleX, pFromScaleY, pToScaleY, null, pEaseFunction);
		}

		public SizeModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final IEntityModifierListener pEntityModifierListener) {
			super(pDuration, pFromScaleX, pToScaleX, pFromScaleY, pToScaleY, pEntityModifierListener, IEaseFunction.DEFAULT);
		}

		public SizeModifier(final float pDuration, final float pFromScaleX, final float pToScaleX, final float pFromScaleY, final float pToScaleY, final IEntityModifierListener pEntityModifierListener, final IEaseFunction pEaseFunction) {
			super(pDuration, pFromScaleX, pToScaleX, pFromScaleY, pToScaleY, pEntityModifierListener, pEaseFunction);
		}

		protected SizeModifier(final SizeModifier pScaleModifier) {
			super(pScaleModifier);
		}

		@Override
		public SizeModifier clone(){
			return new SizeModifier(this);
		}

		// ===========================================================
		// Getter & Setter
		// ===========================================================

		// ===========================================================
		// Methods for/from SuperClass/Interfaces
		// ===========================================================

		@Override
		protected void onSetInitialValues(final IEntity pEntity, final float pScaleA, final float pScaleB) {
			((Rectangle)pEntity).setWidth(pScaleA);
			((Rectangle)pEntity).setHeight(pScaleB);
		}

		@Override
		protected void onSetValues(final IEntity pEntity, final float pPercentageDone, final float pScaleA, final float pScaleB) {
			((Rectangle)pEntity).setWidth(pScaleA);
			((Rectangle)pEntity).setHeight(pScaleB);
		}

		// ===========================================================
		// Methods
		// ===========================================================

		// ===========================================================
		// Inner and Anonymous Classes
		// ===========================================================
	}

}
